import axios from 'axios'
import JSONbig from 'json-bigint'
import {getUserToken} from '@/store/index.js'
const instance = axios.create({
    baseURL:'http://ttapi.research.itcast.cn/',
    transformResponse:[function(data){
      try {
        data = JSONbig.parse(data)
        return data
      } catch (error) {
        console.error(error)
      }
    }]
})

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么?添加token
    const userInfo = getUserToken()
    if(userInfo && userInfo.token){
        config.headers.Authorization = `Bearer ${userInfo.token}`
    }
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

export default instance